package com.uptown.matrix.core.template;

import java.nio.charset.StandardCharsets;

import javax.annotation.Resource;

import com.uptown.matrix.core.annotation.MatrixMessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.stream.StreamListener;
import org.springframework.stereotype.Component;


/**
 * 消费者模板，内部注册topic时会校验topic合法性，不合法收不到消息
 */
@Component
@Slf4j
public class RepeatedRedisGroupConsumer implements StreamListener<String, ObjectRecord<String, Object>> {
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @MatrixMessageListener(streamKey = "uptown", consumerGroup = "uptown_group2", consumerName = "uptown_consumer")
    @Override
    public void onMessage(ObjectRecord<String, Object> message) {
        String msg = new String((byte[]) message.getValue(), StandardCharsets.UTF_8);
        stringRedisTemplate.opsForStream().acknowledge("uptown_group2", message);
        log.info("consumer2 message -> {}", msg);
    }
}
